#include <GL/glew.h>
#include "Line.h"

mgl::Line::Line()
{
	m_vBuffer.resize(12);
	const static float vBuffer[] = {
		0.f,0.f,0.f,
		1.f,0.f,0.f,
		0.f,1.f,0.f,
		0.f,0.f,1.f,
	};
	std::memcpy(&m_vBuffer[0], vBuffer, sizeof(vBuffer));

	m_iBuffer.resize(6);
	const static uint iBuffer[] = {
		0,1,0,2,0,3,
	};
	std::memcpy(&m_iBuffer[0], iBuffer, sizeof(iBuffer));
}

void mgl::Line::createBuffer()
{
	glGenBuffers(1, &m_iBufferId);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_iBufferId);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int)*m_iBuffer.size(), &m_iBuffer[0], GL_STATIC_DRAW);

	glGenBuffers(1, &m_vBufferId);
	glBindBuffer(GL_ARRAY_BUFFER, m_vBufferId);
	glBufferData(GL_ARRAY_BUFFER, sizeof(float)*m_vBuffer.size(), &m_vBuffer[0], GL_STATIC_DRAW);
}

void mgl::Line::releaseBuffer()
{
	glDeleteBuffers(1, &m_iBufferId);
	glDeleteBuffers(1, &m_vBufferId);
}

void mgl::Line::draw() const 
{
	glEnableVertexAttribArray(0);
	glBindBuffer(GL_ARRAY_BUFFER, m_vBufferId);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_iBufferId);
	glDrawElements(GL_LINES, 6, GL_UNSIGNED_INT, nullptr);

	glDisableVertexAttribArray(0);
}
